package com.hackcloud.shardingjdbc.config.pools;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {
    static int core = Runtime.getRuntime().availableProcessors();

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return getPool(core, core * 5, new ThreadFactoryBuilder().setNameFormat("pool-%d").build(), new ThreadPoolExecutor.AbortPolicy());
    }

    public ThreadPoolExecutor getPool(int core, int max, ThreadFactory threadFactory,
                                      RejectedExecutionHandler rejected) {
        return new ThreadPoolExecutor(core, max, 200, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(), threadFactory, rejected);
    }
}
